//
// Copyright (c) 2014, 保留所有权利。
//
// ProjectUtil.java
//
// 作    者：王红燕
// 当前版本：1.0
// 2014 下午2:35:29
//
// 
//
package com.vanke.agent.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.vanke.agent.ADataSource;

/**
 *
 */
public class ProjectUtil {
	static Logger log = Logger.getLogger(ProjectUtil.class);
	private static Map<String, String> projCodeToGuidMap = new HashMap<String, String>();
	private static Map<String, String> projGuidToCodeMap = new HashMap<String, String>();

	static {
		Connection con = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			String sql = "select ProjCode,ProjGUID from p_Project";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String projCode = rs.getString(1);
				String projGuid = rs.getString(2);
				projGuidToCodeMap.put(projGuid, projCode);
				projCodeToGuidMap.put(projCode, projGuid);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("load project error:" + e);
		} finally {
			ADataSource.close(rs, pstmt, con);
		}
	}

	/*
	 * 按项目GUID取项目Code
	 */
	public static String getProjectGuid(Connection myCon, String projCode)
			throws SQLException {
		String projGuid = projCodeToGuidMap.get(projCode);
		if (projGuid == null) {
			projGuid = doProjectGuid(myCon, projCode);
			projCodeToGuidMap.put(projCode, projGuid);
		}
		return projCode;
	}

	/*
	 * 按项目GUID取项目Code
	 */
	public static String getProjectCode(Connection myCon, String projGUID)
			throws SQLException {
		String projCode = projGuidToCodeMap.get(projGUID);
		if (projCode == null) {
			projCode = doProjectCode(myCon, projGUID);
			projGuidToCodeMap.put(projGUID, projCode);
		}
		return projCode;
	} //~getProjectCode

	private static String doProjectCode(Connection con, String projGUID)
			throws SQLException {
		log.info("projGUID .... " + projGUID);
		String projCode = "";
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		try {
			String sql = "select ProjCode from p_Project where ProjGUID=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, projGUID);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				projCode = rs.getString(1);
			} else {
				log.warn("no projCode found for " + projGUID);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("get project code for " + projGUID + " fail:" + e);
		} finally {
			ADataSource.close(rs);
			ADataSource.close(pstmt);
		}
		return projCode;
	} //~getProjectCode

	private static String doProjectGuid(Connection con, String projCode)
			throws SQLException {
		String projGuid = "";
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		try {
			String sql = "select ProjGUID from p_Project where ProjCode=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, projCode);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				projGuid = rs.getString(1);
			} else {
				log.warn("no projGuid found for " + projCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("get project guid for " + projCode + " fail:" + e);
		} finally {
			ADataSource.close(rs);
			ADataSource.close(pstmt);
		}
		return projGuid;
	} //~getProjectCode
}
